package com.gwnet.shop.service.impl;

import java.sql.Time;
import java.util.List;
import java.util.UUID;

import com.gwnet.common.core.utils.DateUtils;
import com.gwnet.shop.domain.TenantOpenhours;
import com.gwnet.shop.mapper.TenantOpenhoursMapper;
import com.gwnet.shop.service.ITenantOpenhoursService;
import com.gwnet.shop.utils.UniqueStringGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gwnet.shop.mapper.SysTenantMapper;
import com.gwnet.shop.domain.SysTenant;
import com.gwnet.shop.service.ISysTenantService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.annotation.Resource;

/**
 * 租户管理Service业务层处理
 * 
 * @author 张万鹏
 * @date 2024-07-25
 */
@Service
public class SysTenantServiceImpl implements ISysTenantService 
{
    @Resource
    private SysTenantMapper sysTenantMapper;

    @Resource
    private TenantOpenhoursMapper tenantOpenhoursMapper;

    @Resource
    private ITenantOpenhoursService tenantOpenhoursService;
    /**
     * 查询租户管理
     * 
     * @param id 租户管理主键
     * @return 租户管理
     */
    @Override
    public SysTenant selectSysTenantById(Long id)
    {
        SysTenant sysTenant = sysTenantMapper.selectSysTenantById(id);

        //查询店铺营业时间段
        TenantOpenhours hour = new TenantOpenhours();
        hour.setTenantId(sysTenant.getId());
        List<TenantOpenhours> hours = tenantOpenhoursMapper.selectTenantOpenhoursList(hour);
        sysTenant.setHours(hours);
        return sysTenant;
    }

    /**
     * 查询租户管理列表
     * 
     * @param sysTenant 租户管理
     * @return 租户管理
     */
    @Override
    public List<SysTenant> selectSysTenantList(SysTenant sysTenant)
    {
        return sysTenantMapper.selectSysTenantList(sysTenant);
    }

    /**
     * 新增租户管理
     * 
     * @param sysTenant 租户管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysTenant(SysTenant sysTenant)
    {
        String tenantId = UniqueStringGenerator.generateUniqueString();
        sysTenant.setTenantId(tenantId);
        sysTenant.setCreateTime(DateUtils.getNowDate());

        int rows = sysTenantMapper.insertSysTenant(sysTenant);
        //添加营业时间段
        tenantOpenhoursService.insertTenantOpenhoursList(sysTenant);
        return rows;
    }

    /**
     * 修改租户管理
     * 
     * @param sysTenant 租户管理
     * @return 结果
     */
    @Override
    public int updateSysTenant(SysTenant sysTenant)
    {
        sysTenant.setUpdateTime(DateUtils.getNowDate());
        return sysTenantMapper.updateSysTenant(sysTenant);
    }

    /**
     * 批量删除租户管理
     * 
     * @param ids 需要删除的租户管理主键
     * @return 结果
     */
    @Override
    public int deleteSysTenantByIds(Long[] ids)
    {
        return sysTenantMapper.deleteSysTenantByIds(ids);
    }

    /**
     * 删除租户管理信息
     * 
     * @param id 租户管理主键
     * @return 结果
     */
    @Override
    public int deleteSysTenantById(Long id)
    {
        return sysTenantMapper.deleteSysTenantById(id);
    }

    /**
     * 查询当前用户购买过的商店列表
     * @param userId
     * @return
     */
    @Override
    public List<SysTenant> selectSysTenantListBuy(Long userId) {
        return sysTenantMapper.selectSysTenantListBuy(userId);
    }

    /**
     * 查询营业中的店铺
     * @param sysTenant
     * @return
     */
    @Override
    public List<SysTenant> selectSysTenantListOpening(SysTenant sysTenant) {
        return sysTenantMapper.selectSysTenantListOpening(sysTenant);
    }
}
